import httpFetch from 'share/httpFetch';
import errorMessage from 'share/errorMessage';
import { deepCopy, messages } from "share/common";

export default {
  //新增自定义报表
  addCustomReport(params) {
    return httpFetch.post(`/report/api/report/user/define`, params)
      .catch((err) => {
        errorMessage(err.response);
      });
  },

  //更新自定义报表
  updateCustomReport(params) {
    return httpFetch.put(`/report/api/report/user/define`, params)
      .catch((err) => {
        errorMessage(err.response);
      });
  },

  //删除自定义报表
  deleteCustomReport(params) {
    return httpFetch.delete(`/report/api/report/user/define`, params)
      .catch((err) => {
        errorMessage(err.response);
      });
  },

  //获取 报表类型
  getReportType() {
    return httpFetch.get(`/report/api/report/category`)
      .catch((err) => {
        errorMessage(err.response);
      });
  },

  //获取 所有报表list
  getReportAllList() {
    return httpFetch.get(`/report/api/report/user/define/management/list`)
      .catch((err) => {
        errorMessage(err.response);
      });
  },

  //自定义报表 详情 接口
  getCustomReportDetail(reportId) {
    return httpFetch.get(`/report/api/report/user/define/detail?reportId=${reportId}`)
      .catch((err) => {
        errorMessage(err && err.response);
      });
  },

  //数据模型列表
  getDataModalList(params) {
    return httpFetch.get(`/report/api/report/user/define/template/report/list`, params)
      .catch((err) => {
        errorMessage(err.response);
      });
  },

  //根据数据模型 id 获取 数据模型的所有属性
  getAllAttributesByDataModalId(params) {
    return new Promise((resolve, reject) => {
      httpFetch.get(`/report/api/report/user/define/template/report/column/list`, params)
        .then((res) => {
          res.data.forEach((modal) => {
            let myModal = modal['reportColumnCustomizedDTOList'] || modal['reportColumnDTOList']
            myModal.forEach((item)=>{
              item.outId = modal.id;
            })
          })
          resolve(res);
        })
        .catch((err) => {
          reject(err);
          errorMessage(err.response);
        });
    })
  },

  //创建好的自定义报表id  创建 单个图表
  addSingleChartByCustomReportId(params) {
    return httpFetch.post(`/report/api/report/user/define/chart`, params)
      .catch((err) => {
        errorMessage(err.response);
      });
  },

  //获取单个图表 的详情
  getSingleChartDetailByItsOwnId(singleChartId) {
    return httpFetch.get(`/report/api/report/user/define/chart/detail?id=${singleChartId}`)
      .catch((err) => {
        errorMessage(err.response);
      });
  },

  //更新单个图表的详情
  updateSingleChartDetail(params) {
    return httpFetch.put(`/report/api/report/user/define/chart`, params)
      .catch((err) => {
        errorMessage(err.response);
      });
  },

  //单个图形表-半屏全屏切换
  switchSingleChartScreenSize(params) {
    return httpFetch.put(`/report/api/report/user/define/chart/switch/screen`, params)
      .catch((err) => {
        errorMessage(err.response);
      });
  },

  //删除单个图表
  deleteSingleChart(params) {
    return httpFetch.delete(`/report/api/report/user/define/chart`, params)
      .catch((err) => {
        errorMessage(err.response);
      });
  },

  //单个图形化报表数据查询（保存后）
  getSingleChartData(reportId, reportChartId, page, size, isPreView, isManualSearch, params) {
    let copyParams = deepCopy(params);
    copyParams.forEach((item, index) => {
      if (copyParams[index] && copyParams[index].orderManId) {
        delete copyParams[index].orderManId;
      }
    });
    let data = {
      reportId,
      reportChartId,
      reportChartSearchDTOs: copyParams
    };
    if (isPreView) {
      data.preview = true;
    }
    if (isManualSearch) {
      data.manualSearch = true;
    }
    return httpFetch.post(`/report/api/report/user/define/search?page=${page}&size=${size}`, data);
    // .catch((err) => {
    //   errorMessage(err.response);
    // })
  },

  //排序接口
  sortChartsSequence(reportId, sortArray) {
    let result = {};
    sortArray.forEach((item, index) => {
      result[index] = item;
    });
    return httpFetch.put(`/report/api/report/user/define/chart/sort?reportId=${reportId}`, result)
      .catch((err) => {
        errorMessage(err.response);
      });
  },

  //获取 可 联动的图形标（同模型）
  getLinkableSingleGraphicIds(params) {
    return httpFetch.get(`/report/api/report/user/define/associate/chart/search`, params)
      .catch((err) => {
        errorMessage(err.response);
      })
  },


  //外部数据源查询
  getOtherDataList({ page = 0, size = 20 }) {
    return httpFetch.get(`/report/api/outside/datasource/report/page`, {page, size})
      .catch((err) => {
        errorMessage(err.response);
      })
  },

  //单个外部数据源详情查询
  getSingleOtherDataDetail({reportId}) {
    return httpFetch.get(`/report/api/outside/datasource/report/detail`, {reportId})
      .catch((err) => {
        errorMessage(err.response);
      })
  },

  //查询单个外部数据源是否被引用   被引用即不可编辑
  querySingleOtherDataHasUse(reportId) {
    return httpFetch.get(`/report/api/outside/datasource/report/validate/${reportId}`)
      .catch((err) => {
        errorMessage(err.response);
      })
  },

  //查询单个外部数据源是否被引用   被引用即不可编辑
  queryCustomModalHasUse(reportId) {
    return httpFetch.get(`/report/api/report/model/validate/${reportId}`)
      .catch((err) => {
        errorMessage(err.response);
      })
  },

  //外部数据源 新建/编辑 接口  后端通过id区分
  newOrUpdateOtherData(params) {
    return httpFetch.post(`/report/api/outside/datasource/report`, params)
      .catch((err) => {
        errorMessage(err.response);
      })
  },

  //自定义数据模型 list 查询
  getAllCustomModalList({ page = 0, size = 20 }) {
    return new Promise((resolve, reject) => {
      httpFetch.get(`/report/api/report/model/list`, {page, size})
        .then((res) => {
          let result = [
            {
              reportName: messages('basic-10.key235')/*自定义数据模型*/,
              id: 'CUSTOM_DATA_MODAL',
              key: 'CUSTOM_DATA_MODAL',
              children: []
            },
            {
              reportName: messages('basic-10.key236')/*系统数据模型*/,
              id: 'SYSTEM_DATA_MODAL',
              key: 'SYSTEM_DATA_MODAL',
              children: []
            }
          ];
          res.data.forEach((item)=>{
            if (item.reportType === 'I') {
              result[1].children.push(item);
            } else if (item.reportType === 'SDRM') {
              result[0].children.push(item);
            }
          })
          res.data = result;
          resolve(res);
        })
        .catch((err) => {
          errorMessage(err.response);
          reject(err);
        })
    })
  },

  //获取自定义数据模型 详情
  getSingleCustomModalDetail({reportId}) {
    return httpFetch.get(`/report/api/report/model/detail/${reportId}`, )
      .catch((err) => {
        errorMessage(err.response);
      })
  },

  //自定义数据模型  新建/编辑 接口  后端通过id区分
  newOrUpdateCustomModal(params) {
    return httpFetch.post(`/report/api/report/model`, params)
      .catch((err) => {
        errorMessage(err.response);
      })
  },
}
